package net.wangds.yunz.initialize.flow;

import net.wangds.auth.module.ResourceSchemaEnum;
import net.wangds.auth.resource.comp.GeneralResourceComp;
import net.wangds.auth.resource.entity.GeneralResourceEntity;
import net.wangds.common.base.funcs.Functions;
import net.wangds.config.comp.SysConfComp;
import net.wangds.jpa.Sorts;
import net.wangds.log.helper.LogHelper;
import net.wangds.procengine.ProcResEnum;
import net.wangds.procengine.flow.FlowContext;
import net.wangds.procengine.flow.define.FlowDef;
import net.wangds.procengine.flow.define.actor.ActorDef;
import net.wangds.procengine.flow.define.node.FlowNode;
import net.wangds.procengine.flow.define.node.JavaFlowNode;
import net.wangds.procengine.flow.instance.actor.Actor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.function.BiFunction;

/**
 * .
 * <p></p>
 *
 * @author 王东石 2020/10/18 9:10.
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class StepInitSystems extends JavaFlowNode {

    public static final String KEY_SYSES = "SYSES";

    @Autowired
    private SysConfComp mmCmpConf;

    @Autowired
    private GeneralResourceComp mmCmpRes;

    public <A extends ActorDef> StepInitSystems(FlowDef<A> flow,
                                                FlowNode previousNode) {
        super(flow, previousNode);
        this.setFunc(this::proc);
    }

    public <A extends ActorDef> StepInitSystems(FlowDef<A> flow,
                                                BiFunction<FlowContext, Actor, ProcResEnum> func,
                                                FlowNode previousNode) {
        super(flow, func, previousNode);
    }

    private ProcResEnum proc(FlowContext ctx, Actor actor) {

        LogHelper.dev("清理现有系统");
        //删除现有应用
        mmCmpRes.deleteInBatch(
                mmCmpRes.findBySchema(ResourceSchemaEnum.SYSTEM, Sorts.createTm_asc)
        );

        LogHelper.dev("初始化现有系统");
        Functions.ofList(
                GeneralResourceEntity.ofSystem("cnpg", "充电桩运维", "", "南方电网充电桩运维", "1", "", 0)

        ).forEach(
                mmCmpRes::save
        );

        ctx.putBean(KEY_SYSES, mmCmpRes.findBySchema(ResourceSchemaEnum.SYSTEM, Sorts.createTm_asc));

        return ProcResEnum.CONTINUE;
    }


}
